<?php

global $_W, $_GPC;
include 'function.php';

$ops = array('list', 'update', 'delete');
$op  = in_array($_GPC['op'], $ops) ? $_GPC['op'] : 'list';

if ($op == 'list') {
    $pageIndex  = max(1, intval($_GPC['page']));
    $pageSize   = 10;
    $total      = pdo_fetchcolumn("SELECT count(*) FROM" . tablename('dgjy_picture') . "WHERE uniacid=:uniacid AND picture_show = 1 ", array(':uniacid' => $_W['uniacid']));
    $pagination = pagination($total, $pageIndex, $pageSize);
    $picture    = pdo_fetchall("SELECT * FROM" . tablename('dgjy_picture') . "WHERE uniacid=:uniacid AND picture_show = 1 ORDER BY picture_id DESC LIMIT " . ($pageIndex - 1) * $pageSize . "," . $pageSize, array(':uniacid' => $_W['uniacid']));

    include $this->template('picture/picture');
} elseif ($op == 'update') {
    if (!empty($_GPC['picture_id'])) {
        $picture = pdo_fetch("SELECT * FROM" . tablename('dgjy_picture') . "WHERE uniacid=:uniacid AND picture_id=:picture_id", array(':uniacid' => $_W['uniacid'], ':picture_id' => $_GPC['picture_id']));
        if (!empty($picture)) {
            if ($picture['uniacid'] == $_W['uniacid']) {
                $res = pdo_update('dgjy_picture', array('picture_pid' => $_GPC['picture_pid']), array('uniacid' => $_W['uniacid'], 'picture_id' => $_GPC['picture_id']));
                if (!empty($res)) {
                    itoast('恭喜！修改成功！', referer(), 'success');
                } else {
                    itoast('抱歉！修改失败！', referer(), 'error');
                }
            } else {
                message('抱歉！您没有权限修改此信息！', $this->createWebUrl('picture'), 'error');
            }
        } else {
            message('抱歉！您要修改的信息不存在！', $this->createWebUrl('picture'), 'error');
        }
    } else {
        message('警告！请勿提交非法网址！', $this->createWebUrl('picture'), 'error');
    }
} elseif ($op == 'delete') {
    if (!empty($_GPC['picture_id'])) {
        $picture = pdo_fetch("SELECT * FROM" . tablename('dgjy_picture') . "WHERE uniacid=:uniacid AND picture_id=:picture_id", array(':uniacid' => $_W['uniacid'], ':picture_id' => $_GPC['picture_id']));
        if (!empty($picture)) {
            if ($picture['uniacid'] == $_W['uniacid']) {
                $res = pdo_delete('dgjy_picture', array('uniacid' => $_W['uniacid'], 'picture_id' => $_GPC['picture_id']));
                if (!empty($res)) {
                    load()->func('file');
                    file_delete($picture['picture_url']);
                    itoast('恭喜！删除成功！', referer(), 'success');
                } else {
                    itoast('抱歉！删除失败！', referer(), 'error');
                }

            } else {
                itoast('抱歉！您没有权限删除此信息！', $this->createWebUrl('picture'), 'error');
            }
        } else {
            message('抱歉！您要删除的信息不存在！', $this->createWebUrl('picture'), 'error');
        }
    } else {
        message('警告！请勿提交非法网址！', $this->createWebUrl('picture'), 'error');
    }
}
?>